package com.example.cymjoe.huaweishopping.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;

import com.example.cymjoe.huaweishopping.SPXQActivity;
import com.example.cymjoe.huaweishopping.WebVieww;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by cymjoe on 2016/12/5.
 */
public class GDTP2Adapter extends PagerAdapter {
    private Context context;

    private List<String> imgNormalList;
    private List<String> prdlList;

    public GDTP2Adapter(Context context, List<String> prdlList, List<String> imgNormalList) {
        this.context = context;
        this.prdlList = prdlList;
        this.imgNormalList = imgNormalList;
    }

    @Override
    public int getCount() {
        return imgNormalList != null ? imgNormalList.size() : 0;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View ret = null;
        ImageView imageView = new ImageView(context);
        imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 500));

        //imageView.setScaleType(ImageView.ScaleType.fix);
        String s = imgNormalList.get(position);
        imageView.setClickable(true);
        Picasso.with(context).load(s).into(imageView);


        final String ss = prdlList.get(position);
        Log.e("ss", ss);
        //http://mw.vmall.com/product/
        String str = new String("http://mw.vmall.com/product/");
        String h = ss.replace(str, "");
        String[] split = h.split("\\.");
        final String s1 = split[0];
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Log.e("ssss", s1);
                if (s1.equals("http://asale")) {
                    Intent intent = new Intent(context, WebVieww.class);
                    intent.putExtra("path", ss);
                    context.startActivity(intent);
                } else {
                    Intent intent = new Intent(context, SPXQActivity.class);


                    intent.putExtra("prdId", s1);


                    context.startActivity(intent);
                }
            }
        });
        ret = imageView;
        container.addView(ret);
        return ret;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {

        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        if (object instanceof View) {
            View view = (View) object;
            container.removeView(view); //移除
        }
    }
}
